import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from "../views/HomeView.vue"
import MyContentView from "../views/MyContentView.vue"

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: "/home",
    meta: {
      index: 0
    }
  },
  {
    path: '/home',
    name: "home",
    component: HomeView,
    meta: {
      index: 0
    }
  },
  {
    path: '/mycontent',
    name: "mycontent",
    component: MyContentView,
    meta: {
      index: 1
    },
    // 独享路由守卫
    beforeEnter: (to, from, next) => {
      let phone = window.localStorage.getItem("phone");
      // 判断是否登录
      if (!phone) {
        next("/login");
        return;
      } else {
        next();
      }
    }
  },
  {
    path: '/userinfo',
    name: 'userinfo',
    component: () => import('../views/UserinfoView.vue'),
    meta: {
      index: 2
    },
    // 独享路由守卫
    beforeEnter: (to, from, next) => {
      let phone = window.localStorage.getItem("phone");
      // 判断是否登录
      if (!phone) {
        next("/login");
        return;
      } else {
        next();
      }
    }
  },
  {
    path: '/classlist',
    name: 'classlist',
    component: () => import('../views/ClassListView.vue'),
    meta: {
      index: 1
    },
    // 独享路由守卫
    beforeEnter: (to, from, next) => {
      let phone = window.localStorage.getItem("phone");
      // 判断是否登录
      if (!phone) {
        next("/login");
        return;
      } else {
        next();
      }
    }
  },
  {
    path: '/dashboard',
    name: 'dashboard',
    component: () => import('../views/DashBoardView.vue'),
    meta: {
      index: 1
    },
    // 独享路由守卫
    beforeEnter: (to, from, next) => {
      let phone = window.localStorage.getItem("phone");
      // 判断是否登录
      if (!phone) {
        next("/login");
        return;
      } else {
        next();
      }
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/loginView.vue'),
    // meta: {
    //   index: 1
    // }
  },
  {
    path: '/stadydone',
    name: 'stadydone',
    component: () => import('../views/StadydoneView.vue'),
    // meta: {
    //   index: 1
    // }
  },
  {
    path: '/vocabulary',
    name: 'vocabulary',
    component: () => import('../views/VocabularybookView.vue'),
    // meta: {
    //   index: 1
    // }
  },
  {
    path: '/waitstady',
    name: 'waitstady',
    component: () => import('../views/WaitstadyView.vue'),
    // meta: {
    //   index: 1
    // }
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/SearchView.vue'),
    // meta: {
    //   index: 1
    // }
  },
  {
    path: '/newstady',
    name: 'newstady',
    component: () => import('../views/NewstadyView.vue'),
    // meta: {
    //   index: 1
    // },
    // 独享路由守卫
    beforeEnter: (to, from, next) => {
      let phone = window.localStorage.getItem("phone");
      // 判断是否登录
      if (!phone) {
        next("/login");
        return;
      } else {
        next();
      }
    }
  },
  {
    path: '/practice',
    name: 'practice',
    component: () => import('../views/practiceView.vue'),
    // meta: {
    //   index: 1
    // },
    // 独享路由守卫
    beforeEnter: (to, from, next) => {
      let phone = window.localStorage.getItem("phone");
      // 判断是否登录
      if (!phone) {
        next("/login");
        return;
      } else {
        next();
      }
    }
  }

  // ClassListView
  // waitstady
  // Search
  // NewstadyView
  // practiceView
]

// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
}



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 配置全局路由守卫
// 用户必须登录才进入  必须登录才可以进入
// router.beforeEach((to,from,next)=>{
//   let token = window.localStorage.getItem("token");
//   // 登录界面判断
//   if(to.path == "/login"){
//       next();
//       return;
//   }else{ //判断是否登录
//     if(!token){
//       next("/login");//进入登录页面
//       return;
//     }else{
//       next();//执行下一个路由
//     }
//   }
// })



export default router